﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utility;

namespace StudentAppointmentPlatform
{
    public partial class ShowHistory : System.Web.UI.Page
    {
        private string loginType;
        private string userId;
        private int uid;

        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
                ViewState["SortOrder"] = "Desc";  //排列顺序;
                ViewState["SortName"] = "CreatTime";  //排列字段;         

                loginType = Session["LoginType"] as string;
                if (string.IsNullOrWhiteSpace(loginType))
                {
                    Alert.AlertOnPage(Page, "请登录后查看!");
                    Redirect.Js_Redirect(Page, "/Default.aspx");
                }

                if (loginType == "Student")
                {
                    History_Title.Text = "我的所有预约记录";
                }
                else if (loginType == "Director")
                {
                    History_Title.Text = "您管理的服务的所有预约记录";
                }

                BindGridView();
            }          
        }

        protected void BindGridView()
        {
            loginType = Session["LoginType"] as string;
            userId = Session["UserId"].ToString();
            uid = UserBO.GetIdByUserId(userId);
            DataTable dt = RecordBO.GetRecordsTable(uid, loginType);
            DataView dv = dt.DefaultView;
            dv.Sort = (string)ViewState["SortName"] + " " + (string)ViewState["SortOrder"];
            gvRecord.DataSource = dv;
            gvRecord.DataBind();         
        }

        protected void gvRecord_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            //string a = e.Row.Cells[0].Text; // 获得的是表头第一格的字符;
            if (-1 == e.Row.RowIndex)
            { // 表头行号序列为-1，跳过它;
                return;
            }
            //string b = DataBinder.Eval(e.Row.DataItem, "ServiceId").ToString(); // 获取数据源中某列值的方法之一;
            string timeBucket = ((DataRowView)e.Row.DataItem).Row["TimeBucket"].ToString(); // 获取数据源中某列值的方法之二;
            timeBucket = TimeBucket.ChangeTimeBucketToShowTime(timeBucket); // 改变值;

            //Literal literal = e.Row.Cells[3].FindControl("lit_TimeBucket") as Literal; // 获取模板列中指定ID的控件;
            Literal literal = e.Row.FindControl("lit_TimeBucket") as Literal; // 可从Row开始寻找控件，避免了使用列序号来定位;
            literal.Text = timeBucket;

            //((DataRowView) e.Row.DataItem).Row["TimeBucket"] = timeBucket; // 此句无效;
            //e.Row.Cells[2].Text = timeBucket; // 此句可对绑定列重新赋值;

            Button btnDetail = e.Row.FindControl("btnDetail") as Button;
            if (loginType == "Director")
            {
                btnDetail.Text = "详情/打印/审批";
            }
            else if (loginType == "Student")
            {
                btnDetail.Text = "详情/打印/评价";
            }

        }

        protected void gvRecord_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                string appointmentId = e.CommandArgument.ToString(); // 方法1：使用CommandArgument获取想要的值;

                //Button sourceButton = (Button)e.CommandSource; // 找到按钮;
                //GridViewRow sourceRow = (GridViewRow)sourceButton.Parent.Parent; // 向上找到所在行;        
                //int rowIndex = sourceRow.RowIndex; // 获取行号;
                //string appointmentId_2 = gvRecord.DataKeys[rowIndex].Values["AppointmentId"].ToString(); // 方法2：配合GridView的 DataKeyNames 来获取想要的字段值;

                Redirect.Js_Redirect2(Page, "/RecordDetail.aspx?AppointmentId=" + appointmentId );
            }
        }

        protected void gvRecord_OnSorting(object sender, GridViewSortEventArgs e)
        {
            string sortName = e.SortExpression;
            if (ViewState["SortName"].ToString() == sortName)
            {
                if (ViewState["SortOrder"].ToString() == "Desc")
                    ViewState["SortOrder"] = "Asc";
                else
                    ViewState["SortOrder"] = "Desc";
            }
            else
            {
                ViewState["SortName"] = sortName;
            }

            BindGridView();
        }
    }
}